package com.ruoyi.mymod.service.impl;

import com.ruoyi.mymod.service.IFeedAccessService;
import com.ruoyi.mymod.service.IFeedService;
import com.ruoyi.mymod.service.IMessageService;
import com.ruoyi.mymod.utils.Dbutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.time.LocalDate;
import java.time.LocalTime;

/**
 * 菜单 业务层处理
 *
 * @author ruoyi
 */
@Service
public class FeedServiceImpl implements IFeedService {


    public static final String PREMISSION_STRING = "perms[\"{0}\"]";

    @Override
    public String getDate(String nowweight,String outweight,String qty) {
        String url = "jdbc:oracle:thin:@10.104.8.7:1521:orcl"; // 使用服务名
        String user = "psi";
        String password = "psi";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String date="0";
        try {
            // 加载Oracle JDBC驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");

            // 建立数据库连接
            conn = DriverManager.getConnection(url, user, password);

            // 创建Statement对象
            stmt = conn.createStatement();

            // 执行SQL查询
            String sql = "select pigpos.p.wgh2aged("+outweight+")-pigpos.p.wgh2aged("+nowweight+") from dual";
            rs = stmt.executeQuery(sql);

            // 处理查询结果
            if (rs.next()){
                date=rs.getString(1);
            }

            if (date==null){
                date="0";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }

        return date;
    }


    @Override
    public Dbutil.R getPrediction(String selectPigtype,String nowweight,String outweight,String qty,String distance) throws Exception {

        Dbutil.R r=null;
        if ("yf".equals(selectPigtype)){
            Object o= new Dbutil.Qp().setUsrpwd("ctcnfd","ctcnfd").
                    setP(Integer.parseInt(nowweight),Integer.parseInt(outweight),Integer.parseInt(qty),Integer.parseInt(distance)).funcP(4,"app.outcostw");
            r=(Dbutil.R) o;
        }else if ("zz".equals(selectPigtype)){
            Object o= new Dbutil.Qp().setUsrpwd("ctcnfd","ctcnfd").
                    setP(Integer.parseInt(qty),Integer.parseInt(distance)).funcP(4,"app.outsowcyclecost");
            r=(Dbutil.R) o;

        }
        return r;

    }

}
